package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.AppUser;
import com.ruoyi.web.domain.UserCapital;
import com.ruoyi.web.service.IAppUserService;
import com.ruoyi.web.service.IUserCapitalService;
import com.ruoyi.web.vo.UserCapitalVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户资金账户Controller
 *
 * @author ruoyi
 * @date 2022-12-30
 */
@RestController
@RequestMapping("/capital")
public class UserCapitalController extends BaseController {
    @Autowired
    private IUserCapitalService userCapitalService;
    @Autowired
    private IAppUserService appUserService;


    @GetMapping("/balance")
    public R<BigDecimal> getUserBalance() {
        return userCapitalService.getUserBalance();
    }

    /**
     * 查询用户资金账户列表
     */
    @PreAuthorize("@ss.hasPermi('web:capital:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCapital userCapital) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser.getUser().getRoles().get(0).getRoleId() == 3) {
            // 获取所有子集
            List<AppUser> userList = new ArrayList<>();
            appUserService.getAllChild(userList, loginUser.getUserId());
            List<Long> userIdList = userList.stream()
                    .map(AppUser::getId)
                    .collect(Collectors.toList());
            if (CollectionUtils.isEmpty(userIdList)) {
                return getDataTable(new ArrayList<>());
            }

            List<UserCapital> list = userCapitalService.listByUserIdList(userIdList, userCapital);
            TableDataInfo info = getDataTable(list);

            List<UserCapitalVO> voList = new ArrayList<>(list.size());
            for (UserCapital i : list) {
                UserCapitalVO vo = new UserCapitalVO();
                BeanUtils.copyProperties(i, vo);
                voList.add(vo);
            }

            return getDataTable(info, voList);

        } else {
            startPage();
            List<UserCapital> list = userCapitalService.selectUserCapitalList(userCapital);
            TableDataInfo info = getDataTable(list);

            List<UserCapitalVO> voList = new ArrayList<>();
            for (UserCapital user : list) {
                UserCapitalVO vo = new UserCapitalVO();
                BeanUtils.copyProperties(user, vo);
                voList.add(vo);
            }
            return getDataTable(info, voList);
        }
    }


    /**
     * 导出用户资金账户列表
     */
    @PreAuthorize("@ss.hasPermi('web:capital:export')")
    @Log(title = "用户资金账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCapital userCapital) {
        List<UserCapital> list = userCapitalService.selectUserCapitalList(userCapital);
        ExcelUtil<UserCapital> util = new ExcelUtil<UserCapital>(UserCapital.class);
        util.exportExcel(response, list, "用户资金账户数据");
    }

    /**
     * 获取用户资金账户详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:capital:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        UserCapital capital = userCapitalService.selectUserCapitalById(id);
        UserCapitalVO vo = new UserCapitalVO();
        BeanUtils.copyProperties(capital, vo);
        return success(vo);
    }

    /**
     * 新增用户资金账户
     */
    @PreAuthorize("@ss.hasPermi('web:capital:add')")
    @Log(title = "用户资金账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCapital userCapital) {
        return toAjax(userCapitalService.insertUserCapital(userCapital));
    }

    /**
     * 修改用户资金账户
     */
    @PreAuthorize("@ss.hasPermi('web:capital:edit')")
    @Log(title = "用户资金账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCapital userCapital) {
        return toAjax(userCapitalService.updateUserCapital(userCapital));
    }

    /**
     * 删除用户资金账户
     */
    @PreAuthorize("@ss.hasPermi('web:capital:remove')")
    @Log(title = "用户资金账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userCapitalService.deleteUserCapitalByIds(ids));
    }
}
